<?

require_once SITE_ROOT_PATH."/_lib/classes/class.holiday_references.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotels.php";

class holiday_special_offers {



// FUNCTION // get all the special offers added to the system
function get_specials($params=''){


	$params = in($params);

	if($params['limit']) $sql_limit = " LIMIT ".$params['limit']." ";
	if($params['order']=='random'){
		$sql_order = " ORDER BY RAND() ";
	}else{
		$sql_order = " ORDER BY S.special_offer_id DESC ";
	}
	
	$sql = "SELECT S.special_offer_id, S.hotel_tree_id, S.reference_id, S.date_expiry, S.date_added, H.string
	FROM holiday_special_offers AS S
	INNER JOIN holiday_references AS H ON S.reference_id = H.reference_id
	WHERE 1 = 1 
	".$sql_where."
	".$sql_order."
	".$sql_limit."";
	$cache = array('seconds'=>'600');
	return apply_query($sql,$wtf,$cache);
	

}







// FUNCTION // holiday results based on special_offer_id
function get_special($params){

	if(!empty($params)){
	
		if($params['special_offer_id']) $sql_where .= " AND special_offer_id = '".$params['special_offer_id']."' ";
		if($params['reference_id']) $sql_where .= " AND reference_id = '".$params['reference_id']."' ";
	
		$sql = "SELECT *
		FROM holiday_special_offers
		WHERE 1=1
		".$sql_where."
		LIMIT 1"; 
	
		return apply_query($sql);
	
	}

}





// FUNCTION // Validate form
function validate_form($results){

	// error variable
	$errors = array();
	
	// get hotel details
	$tree = new tree();
	$params['tree_id'] = $results['hotel_tree_id'];
	$hotel_values = $tree->get_branch($params);
	$hotel_values = $hotel_values[0];
	if(empty($results['tour_op'])) array_push($errors, 'Please enter the tour op');
	if(empty($hotel_values)) array_push($errors, 'Hotel Tree ID not found');
	if(empty($results['nights'])) array_push($errors, 'Please enter the number of nights');
	if(empty($results['board_basis'])) array_push($errors, 'Please enter the borad basis');
	// Departure date
	$departure_date_array = explode('-',$results['departure_date_day']."-".$results['departure_date_month']);
	// Check date is valid
	if(!checkdate((int)$departure_date_array[1], (int)$departure_date_array[0], (int)$departure_date_array[2])){
		array_push($errors, 'Please enter a valid departure date');
	}	
	// Check date is later then today
	$departure_date_timestamp = mktime(0, 0, 0, (int)$departure_date_array[1], (int)$departure_date_array[0], (int)$departure_date_array[2]);
	if($departure_date_timestamp <= strtotime("now")){
		array_push($errors, 'Please enter a departure date in the future');
	}
	
	// Return date
	$return_date_array = explode('-',$results['return_date_day']."-".$results['return_date_month']);
	// Check date is valid
	if(!checkdate((int)$return_date_array[1], (int)$return_date_array[0], (int)$return_date_array[2])){
		array_push($errors, 'Please enter a valid return date');
	}	
	// Check date is later then today
	$return_date_timestamp = mktime(0, 0, 0, (int)$return_date_array[1], (int)$return_date_array[0], (int)$return_date_array[2]);
	if($return_date_timestamp < $departure_date_timestamp){
		array_push($errors, 'Please enter a return after the departure date');
	}
	

	if(empty($results['departure_airport'])) array_push($errors, 'Please enter the departure airport');
	if(empty($results['destination_airport'])) array_push($errors, 'Please enter the destination airport');
	if(empty($results['return_airport'])) array_push($errors, 'Please enter the return airport');
	if(empty($results['price_adult'])) array_push($errors, 'Please enter the holiday price');
	

	return $errors;
	
}





// FUNCTION // Validate form
function validate_form_ref($results){

	// get holiday details
	$holiday_references = new holiday_references();
	$holiday_values = $holiday_references->get_holiday($results['reference_id']);

	// error variable
	$errors = array();

	if(empty($results['reference_id'])) array_push($errors, 'Please enter the reference id');
	if(!$holiday_values) array_push($errors, 'No holiday exsists with the '.$params['reference_id']);
	
	if($holiday_values){
		// Look up hotel
		$hotels = new hotels();
		$params = $holiday_values;
		$params['supplier_code'] = 'mcs';
		$hotel_values = $hotels->lookup_hotel($params);
		if(!$hotel_values) array_push($errors, 'There is no hotel linked to this offer');
	}

	return $errors;
	
}




// FUNCTION // delete special offer
function delete_special_offer($special_offer_id){

	if(!empty($special_offer_id)){
		
		$special_offer_id = in($special_offer_id);
		
		$sql = "SELECT reference_id FROM holiday_special_offers
		WHERE special_offer_id = '".$special_offer_id."' 
		LIMIT 1 ";
		
		$special_result = apply_query($sql);

		$sql = "DELETE FROM holiday_special_offers
		WHERE special_offer_id = '".$special_offer_id."'";
		
		$result = mysql_query($sql) or die (mysql_error());
		
		$sql = "DELETE FROM holiday_references
		WHERE reference_id = '".$special_result[0]['reference_id']."' LIMIT 1 ";

		$result = mysql_query($sql) or die (mysql_error());


	}

}



// FUNCTION // insert offer details
function insert_offer($params){

if(!empty($params)){

	// use safe function
	$params = in($params);
	
	$sql = "INSERT INTO holiday_special_offers
	SET	reference_id = '".$params['reference_id']."',
	hotel_tree_id = '".$params['hotel_tree_id']."',
	date_expiry = '".$params['date_expiry']."',
	date_added = '".mktime()."' "; 
	
	$result = mysql_query($sql) or die (mysql_error());
	
}
	
}






// FUNCTION // Update the offer details
function update_offer($params){

	if(!empty($params)){
	
		$params = in($params);
		
		$sql = "UPDATE holiday_special_offers
		SET	reference_id = '".$params['reference_id']."',
		date_expiry = '".$params['date_expiry']."',
		hotel_tree_id = '".$params['hotel_tree_id']."'
		WHERE special_offer_id = '".$params['special_offer_id']."' "; 
		
		$result = mysql_query($sql) or die (mysql_error());
	
	}


}
/*

// FUNCTION // get all the special offers added to the system including holiday details
function get_full_specials($params=''){


	$params = in($params);

	if($params['limit']) $sql_limit = " LIMIT ".$params['limit']." ";
	if($params['order']=='random'){
		$sql_order = " ORDER BY RAND() ";
	}else{
		$sql_order = " ORDER BY special_offer_id DESC ";
	}
	
	$sql = "SELECT special_offer_id, hotel_tree_id, holiday_special_offers.reference_id, string
	FROM holiday_special_offers, holiday_references
	WHERE holiday_special_offers.reference_id = holiday_references.reference_id 
	".$sql_where."
	".$sql_order."
	".$sql_limit."";

	return apply_query($sql);

}
*/



}

?>